<?php

namespace app\api\controller;

use think\Controller;
use think\Config;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
use think\Response;
/**
 * 矿工接口
 */
class Base extends controller
{
	protected $user;
	protected $responseType = 'json';
    protected function _initialize()
    {
    	$token = input('token');
    	if(empty($token)){
    		return json(['data'=>'','code'=>99,'msg'=>'请先登录']);
    	}
    	$user = db('user')->where(['token'=>$token])->find();
    	if(!$user){
    		return json(['data'=>'','code'=>99,'msg'=>'您的账号已在别处登录']);
    	}
    	$this->user = $user;
    }
     /**
     * 操作成功返回的数据
     * @param string $msg   提示信息
     * @param mixed $data   要返回的数据
     * @param int   $code   错误码，默认为1
     * @param string $type  输出类型
     * @param array $header 发送的 Header 信息
     */
    protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }

    /**
     * 操作失败返回的数据
     * @param string $msg   提示信息
     * @param mixed $data   要返回的数据
     * @param int   $code   错误码，默认为0
     * @param string $type  输出类型
     * @param array $header 发送的 Header 信息
     */
    protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }
    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
        ];
        // 如果未设置类型则自动判断
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);

        if (isset($header['statuscode']))
        {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        }
        else
        {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }
    /**
     * 加入财务日志
     */
    public function addfinancial($user_id,$type=1,$desc='',$status=1,$glod_num=0,$silver_num=0,$strength=0,$miners_id=0){
        $data['user_id'] = $user_id;
        $data['type'] = $type;
        $data['desc'] = $desc;
        $data['status'] = $status;
        $data['glod_num'] = $glod_num;
        $data['silver_num'] = $silver_num;
        $data['strength'] = $strength;
        $data['miners_id'] = $miners_id;
        $data['createtime'] = time();
        $r = db('user_log')->insert($data);
    }
}
